import AntModal from '@/components/pt/dialog/AntModal'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import SelectUser from '@/components/pt/selectUser/SelectUser'
import { addBizAfterSaleOrder, updateBizAfterSaleOrder,remove,updateBizAfterUser } from '@/api/biz/bizAfterSaleOrder'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { upload } from '@/api/system/upload'
import BizOrderSelect  from "./BizOrderSelect"
export default {
  name: 'CreateForm',
  components: {
    AntModal,
    AdvanceTable,
    SelectUser,
    BizOrderSelect
  },
  data () {
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 14 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '新增售后单',
      headers: { Authorization: 'Bearer ' + storage.get(ACCESS_TOKEN) },
      // 表单参数
      form: {},
      rules: {
        customerId: [{ required: true, message: '客户不能为空', trigger: 'blur' }],
        orderNumber: [{ required: true, message: '请关联销售订单', trigger: 'blur' }],
        feedAmount: [{ required: true, message: '不能为空', trigger: 'blur' }],
        orderType: [{ required: true, message: '订单类型不能为空', trigger: 'blur' }]
      },
      list: [],
      selectedRowKeys:[],
      ids:[],
      fileList: []
    }
  },
  filters: {},
  created () {},
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    selectOk(selectedRows){
      console.log('selectedRows :>> ', selectedRows);
      this.form.orderNumber=selectedRows[0].orderNumber;
      this.form.customerName=selectedRows[0].customerName;
      this.form.address=selectedRows[0].address;
      this.form.phone=selectedRows[0].phone;
      this.form.orderId=selectedRows[0].id;
      this.$forceUpdate();
    },
    onSearchLinkNumber(){
      this.$refs.BizOrderSelect.show()
    },
    onClose () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset () {
      this.form = {}
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.open = true
      this.formTitle = row.id ? '修改售后单' : '新增售后单';
      this.form = row
      if(row.id) {
        this.fileList=[];
        row.saleOrderAttaches.forEach((file)=>{
          this.fileList.push({
            url:file.attachPath,
            status:"done",
            uid:Math.random(),
            name:file.attachPath
          })
        })
      }
    },
    handleUser(row){
      this.reset()
      this.open = true
      this.formTitle = '选择处理人';
      this.form = row
    },
    handleChange(info, e) {
      const formData = new FormData();
      formData.append('file', info.file);
      upload(formData).then(response => {
        const d = {
          uid: info.file.uid,
          name: response.fileName,
          url: response.url,
          status: 'done'
        }
        this.fileList.push(d);
      })
    },
    handleRemove(info) {
      this.fileList = this.fileList.filter(function (item) {
        return item.uid != info.uid
      })
    },
    remove:function() {
      //删除
      let that = this;
      that.$confirm({
        title: `确认删除所选中数据?`,
        onOk() {
          return remove(that.form.id).then((res) => {
            if (res.code === 200) {
              that.$message.success('删除成功', 3)
              that.open = false
              that.$emit('ok')
            }
          })
        },
        onCancel() { },
      })
    },
    submitFormUser(closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.id !== undefined) {
            if (this.form.processUserId) {
              saveForm.processUserId = this.form.processUserId.ids
            }
            updateBizAfterUser(saveForm).then(response => {
              this.$message.success('更新成功', 3)
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
            }).catch(()=>{
              
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.id !== undefined) {
            if (this.form.processUserId) {
              saveForm.processUserId = this.form.processUserId.ids
            }
            saveForm.saleOrderAttaches=[];
            this.fileList.forEach((file)=>{
              saveForm.saleOrderAttaches.push({
                'attachPath':file.url,
                'attachType':1
              })
            })
            updateBizAfterSaleOrder(saveForm).then(response => {
              this.$message.success('更新成功', 3)
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
            }).catch(()=>{
              
              this.disabled = false
            })
          } else {
            saveForm.saleOrderAttaches=[];
            this.fileList.forEach((file)=>{
              saveForm.saleOrderAttaches.push({
                'attachPath':file.url,
                'attachType':1
              })
            })
            addBizAfterSaleOrder(saveForm).then(response => {
              this.$message.success('新增成功', 3)
                this.$emit('ok')
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
            }).catch(()=>{
              
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back () {
      const index = '/biz/bizpurchase/index'
      this.$router.push(index)
    },
  }
}
